package com.viari.calort;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ViewFlipper;

public class MainActivity extends Activity {
	static final int DIALOG_ABOUT_ID = 0;
	static final int DIALOG_EXIT_ID = 1;

	private ViewFlipper flipper;
    private GestureDetector gestureDetector;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        flipper = (ViewFlipper) findViewById(R.id.viewFlipperID);
        final Calendar c = Calendar.getInstance();
        
        flipper.setDisplayedChild( c.get(Calendar.MONTH) );

        SimpleOnGestureListener gestureListener = new SimpleOnGestureListener() {
        	private static final int SWIPE_MIN_DISTANCE = 60;
        	//private static final int SWIPE_MAX_OFF = 80;
        	private static final int SWIPE_THRESHOLD_VELOCITY = 100;

        	@Override
        	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        		if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    flipper.setInAnimation(inFromRightAnimation());
                    flipper.setOutAnimation(outToLeftAnimation());
                    flipper.showNext(); 
        		} 
        		else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    flipper.setInAnimation(inFromLeftAnimation());
                    flipper.setOutAnimation(outToRightAnimation());
                    flipper.showPrevious();
        		}
        		return true;
        	};
        };
        
        gestureDetector = new GestureDetector(gestureListener);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.exit:
        	showDialog( DIALOG_EXIT_ID );
            return true;
        case R.id.back:
            flipper.setInAnimation(inFromLeftAnimation());
            flipper.setOutAnimation(outToRightAnimation());
        	flipper.showPrevious();
            return true;
        case R.id.next:
            flipper.setInAnimation(inFromRightAnimation());
            flipper.setOutAnimation(outToLeftAnimation());
       	 	flipper.showNext();
            return true;
        case R.id.info:
        	showDialog( DIALOG_ABOUT_ID );
            return true; 
        case R.id.settings:
            Intent i = new Intent(this, Settings.class);
            startActivity(i);
        	return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
      if (gestureDetector.onTouchEvent(event)) {
        return true;
      }
      else {
        return false;
      }
    }
    
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev){
        super.dispatchTouchEvent(ev);
        return gestureDetector.onTouchEvent(ev);
    }
    
    private Animation inFromRightAnimation() {

    	Animation inFromRight = new TranslateAnimation(
    	Animation.RELATIVE_TO_PARENT,  +1.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
    	Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
    	);
    	inFromRight.setDuration(500);
    	inFromRight.setInterpolator(new AccelerateInterpolator());
    	return inFromRight;
    }
    
    private Animation outToLeftAnimation() {
    	Animation outtoLeft = new TranslateAnimation(
    	 Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  -1.0f,
    	 Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
    	);
    	outtoLeft.setDuration(500);
    	outtoLeft.setInterpolator(new AccelerateInterpolator());
    	return outtoLeft;
    }
    
    private Animation inFromLeftAnimation() {
    	Animation inFromLeft = new TranslateAnimation(
    	Animation.RELATIVE_TO_PARENT,  -1.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
    	Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
    	);
    	inFromLeft.setDuration(500);
    	inFromLeft.setInterpolator(new AccelerateInterpolator());
    	return inFromLeft;
    	}
    
    private Animation outToRightAnimation() {
    	Animation outtoRight = new TranslateAnimation(
    	 Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  +1.0f,
    	 Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
    	);
    	outtoRight.setDuration(500);
    	outtoRight.setInterpolator(new AccelerateInterpolator());
    	return outtoRight;
    	}  
    
    @Override
    protected Dialog onCreateDialog(int id) {
    	Dialog dialog;
        switch(id) {
        case DIALOG_ABOUT_ID:
        	dialog = new Dialog ( MainActivity.this );
        	dialog.setContentView(R.layout.about_dialog);
        	dialog.setTitle("Calendar Ortodox 2011");
        	dialog.setCancelable(true);
        	dialog.show();
        	break;
        case DIALOG_EXIT_ID:
        	AlertDialog.Builder builder = new AlertDialog.Builder(this);
        	builder.setMessage("Are you sure you want to exit?");
        	builder.setCancelable(false);
        	builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        	           public void onClick(DialogInterface dialog, int id) {
        	                MainActivity.this.finish();
        	           }
        	       })
        	       .setNegativeButton("No", new DialogInterface.OnClickListener() {
        	           public void onClick(DialogInterface dialog, int id) {
        	                dialog.cancel();
        	           }
        	       });
        	dialog = builder.create();
        	dialog.show();
        default:
            dialog = null;
        }
        return dialog;
    }
    
    
}